<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <script src="js/jquery-2.1.1.min.js"></script>
    <script src="js/loading.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <script src="js/commonTool.js"></script>
<!--    <script src="js/jquery.filtertable.js"></script>-->
<!--    <script src="js/headerLoad.js"></script> -->
    <script>
    $(function(){
        $("#header").load("header.html");
        var popview=$("#popbackView");
        var addToken=Tool.addTokenToParams;
        (function(){
            $.ajax({
                type: 'GET',
                url:doGetAllCompanyUser,
                contentType:"application/json;charset=UTF-8",
    		    xhrFields: {
    		    withCredentials: true
    		           },
    		    crossDomain: true,
                beforeSend:function(xhr){
                    showLoading();
                    //发送请求前运行的函数
                },
                success:function(result){
                    console.log(result);
                    var usersArr=result.data;
                    usersArr=usersArr.length>0?usersArr:[];
                    $("#user_tb").children().each(function(){
                       $(this).remove(); 
                    });
                    for(var i=0;i<usersArr.length;i++){
                        var $tr=$('<tr></tr>');
                        var $td_uid=$('<td></td>');
                        $td_uid.text(usersArr[i].userid);
                        var $td_uname=$('<td></td>');
                        $td_uname.text(usersArr[i].username);
                        var $td_ubm=$('<td></td>');
                        $td_ubm.text(usersArr[i].userPartment);
                        var $td_utel=$('<td></td>');
                        $td_utel.text(usersArr[i].usertel);
                        var $td_btn=$('<td><span>修改</span></td>');
                        $tr.append($td_uid,$td_uname,$td_ubm,$td_utel,$td_btn);
                        $("#user_tb").append($tr);
                    }

                    //当请求成功时运行的函数
                },
                error:function(xhr,status,error){
                    console.log(status.toString()+error.toString());
                    //如果请求失败要运行的函数
                },
                complete:function(xhr,status){
                    hideLoading();
                    spanClickInitFunc();
             }
            });
        })();
        //user data load.
    var spanClickInitFunc=function(){
        $("#user_table span").click(function(){
            if(popview.css("display")!="none"){
                return;
            }
            popview.css('display','block');
            u_id=$(this).parent().siblings().eq(0).text();
            var params=addToken({userId:u_id});
            $.ajax({
                url:doGetUser,
                type:"POST",
                data:JSON.stringify(params),
                contentType:"application/json;charset=UTF-8",
    		    xhrFields: {
    		    withCredentials: true
    		           },
    		    crossDomain: true,
                beforeSend:function(xhr){
                    showLoading();
                    //发送请求前运行的函数
                },
                success:function(result){
                    console.log(result);
                    var data=result.data;
                    var canManageTar="0",canSubmitTar="0",canVisitAna="0",isAdmin="0";
                    if(result.result_code=='0000'){
                        canManageTar=data.canManageTarget;
                        canSubmitTar=data.canSubmitTarget;
                        canVisitAna=data.canVisitAnalytic;
                        isAdmin=data.isAdmin
                    }
                    
                    $("#tar_submit").prop("checked",Boolean(parseInt(canSubmitTar))); 
                    $("#tar_manage").prop("checked",Boolean(parseInt(canManageTar))); 
                    $("#tar_scan").prop("checked",Boolean(parseInt(canVisitAna))); 
                    $("#tar_admin").prop("checked",Boolean(parseInt(isAdmin)));
                },
                error:function(xhr,status,error){
                    console.log(status.toString()+error.toString());
                    //如果请求失败要运行的函数
                },
                complete:function(xhr,status){
                    hideLoading();
                      //  请求完成时运行的函数,所有回调之后
                }
            });
        });
        };
        
        $("#sendbtn").click(function(){
            if(confirm('确定要提交吗?')){
                var userId=u_id;
                var submitTag=$("#tar_submit").is(':checked')?"1":"0";
                var manageTag=$("#tar_manage").is(':checked')?"1":"0";
                var scanTag=$("#tar_scan").is(':checked')?"1":"0";
                var adminTag=$("#tar_admin").is(':checked')?"1":"0";
                var sparam={userId:userId,isManage:manageTag,isSubmit:submitTag,isVisit:scanTag,isAdmin:adminTag};
                sparam=addToken(sparam);
                $.ajax({
                    url:doSetUser,
                    type:"POST",
                    data:JSON.stringify(sparam),
                    contentType:"application/json;charset=UTF-8",
    		        xhrFields: {
    		        withCredentials: true
    		          },
    		        crossDomain: true,
                    beforeSend:function(xhr){
                        showLoading();
                        //发送请求前运行的函数
                    },
                    success:function(result){
                        console.log(result);
                        if(result.result_code=='0000'){
                            alert('恭喜,修改权限成功!');
                            delete u_id;
                            $("#close").trigger('click');
                            return;
                        }else{
                            alert(result.result_msg);
                        }

                        //当请求成功时运行的函数
                    },
                    error:function(xhr,status,error){
                        console.log(status.toString()+error.toString());
                        alert("服务器产生错误啦!");
                        //如果请求失败要运行的函数
                    },
                    complete:function(xhr,status){
                        hideLoading();
                          //  请求完成时运行的函数,所有回调之后
                    }
                });
            }
            
        });
        $("#close").click(function(){
           if(popview.css("display")=="block"){
               popview.css('display','none');
            }
        });
    });
    </script>
    <link rel="stylesheet" href="css/mainstyle.css">
    <link rel="stylesheet" href="css/style.css">
    <style>
        #cont_title{
            position: relative;
            width: 100%;
            height: 40px;
            background-color: dodgerblue;
            color: white;
            
        }
        .tit{
            position: absolute;
            height: 20px;
            top: 50%;
            left: 10px;
            margin-top: -10px;
        }
        
        td.alt { background-color: #ffc; background-color: rgba(230, 127, 34, 0.2); }
        #user_table{
            width: 100%;
            text-align: center;
            vertical-align: middle;
            border-color: lightgray;
            font-size: 13px;
        }
        #user_table span{
            display: block;
/*            width: 30%;*/
            margin: 0 auto;
            background-color: dodgerblue;
            color: white;
        }
        #user_table tr{
            height: 30px;
        }
        #popbackView{
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index:100;
            display:none;
        }
        #popView{
            position: fixed;
            width: 250px;
            height: 300px;
            left: 50%;
            top: 50%;
            margin-left: -125px;
            margin-top: -190px;
            background-color: white;
            z-index: 100;
            border-radius: 5px;
            text-align: center;
        }
        #close{
            font-size: 20px;
            width: 30px;
            height: 30px;
            margin-left: 240px; 
            margin-top: -15px;
            border-radius: 15px;
            background-color: dodgerblue;
            border: 0;
            color: white;    
        }
        #permission_table{
            width: 100%;
            text-align: left;
        }
        .qh{
        position: absolute;
        display: block;
        right: 5px;
        top: 50%;
        margin-top: -10px;
        text-decoration-line: underline;
        font-size: 15px;
        color: white;
    }
        
    </style>
    <title>权限设置</title>
</head>
<body>
<div id="header"></div>
<div id="content">
    <div id="cont_title"><span class="tit">权限设置</span><a href="newPerSet.html"><span class="qh">切换新版授权</span></a></div>
    <table id="user_table"  border="1" cellspacing="0">
        <thead>
            <tr>
			 <th scope="col" title="员工列表">#</th>
			 <th scope="col">名字</th>
			 <th scope="col">部门</th>
			 <th scope="col">手机号</th>
			 <th scope="col">权限设置/查询</th>
			  </tr>
        </thead>
        <tbody id="user_tb" valign="middle">
        </tbody>
    </table>	
</div>
<div id="popbackView">
    <div id="popView">
        <button id="close">X</button>
        <span style="font-size: 17px;font-weight:bold;">设置</span>
        <table id="permission_table">
            <thead>
                <tr>
                    <th scope="col" title="platform">权限</th>
                    <th scope="col">是/否</th>
                </tr>
            </thead>
            <tbody id="sq_tb" valign="middle">
                <tr><td>计划录入:</td><td><input id="tar_submit" type="checkbox"></td></tr>
                <tr><td>计划管理:</td><td><input id="tar_manage"
 type="checkbox"></td></tr>
                <tr><td>查看流量:</td><td><input id="tar_scan"
 type="checkbox"></td></tr>
                <tr><td>管理员:</td><td><input id="tar_admin" type="checkbox"></td></tr>
            </tbody>
        </table>
        <br>
        <button id="sendbtn">提交</button>
    </div>
    </div>
</body>
</html>